#define _CRT_SECURE_NO_WARNINGS  1
//一定要动手，例子不管多么简单，建议至少自己手敲一遍看看是否理解了里头的细枝末节。
//一定要学会思考，思考为什么要这样，而不是那样。还要举一反三地思考
#include"Link_List.h"
SListNode* BuySListNode(SLTDateType x)
{
	SListNode* node = (SListNode*)malloc(sizeof(SListNode));
	if (node == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	node->data = x;
	node->next = NULL;
	return node;
}
void SListPrint(SListNode* plist)
{
	assert(plist);
	while (plist->next != NULL)
	{
		printf("%d ", plist->data);
		plist = plist->next;
	}
	printf("%d ", plist->data);
}
void SListPushBack(SListNode** pplist, SLTDateType x)
{
	assert(*pplist);
	SListNode* node = BuySListNode(x);
	SListNode* tail = *pplist;
	while (tail->next != NULL)
	{
		tail = tail->next;
	}
	tail->next = node;
}
void SListPushFront(SListNode** pplist, SLTDateType x)
{
	assert(*pplist);
	SListNode* node = BuySListNode(x);
	SListNode* tail = *pplist;
	if (tail == NULL)
	{
		return node;
	}
	else
	{
		node->next = tail;
		tail = node;
		free(tail);
	}
}
void SListPopBack(SListNode** pplist)
{
	assert(*pplist);
	SListNode* tail = *pplist;
	while (tail->next->next != NULL)
	{
		tail = tail->next;
	}
	tail->next = NULL;
	free(tail);
}
void SListPopFront(SListNode** pplist)
{
	assert(*pplist);
	SListNode* tail = *pplist;
	if (tail == NULL)
	{
		return;
    }
	else if (tail->next == NULL)
	{
		tail = NULL;
	}
	else
	{
		tail = tail->next;
	}
	free(tail);
}
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	assert(plist);
	while (plist->data != x)
	{
		plist = plist->next;
		if (plist == NULL)
		{
			break;
		}
	}
	if (plist == NULL)
	{
		return NULL;
	}
	return plist;
}
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
	assert(pos);
	SListNode* node = BuySListNode(x);
	node->next = pos->next;
	pos->next = node;
}
void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	SListNode* tail = pos->next;
	if (tail != NULL)
	{
		SListNode* tailnext = tail->next;
		free(tail);
		pos->next = tailnext;
	}
}
void SListDestroy(SListNode* plist)
{
	assert(plist);
	SListNode* tail = NULL;
	while (plist->next != NULL)
	{
		tail = plist->next;
		free(plist);
		plist = tail;
	}
	free(plist);
}